package com.weiaibenpao.demo.chislim;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.orhanobut.logger.Logger;

public class LocationUtil  implements AMapLocationListener {
    private AMapLocationClient mLocationClient = null;
    private OnLocationListener mOnLocationListener;

    /**
     * 注意:为适配6.0及以上系统不管全局定位还是局部定位必须判断已经获得了定位权限的情况下才启动定位
     */
    public LocationUtil(Context context) {
        this.mLocationClient = new AMapLocationClient(context);
        this.mLocationClient.setLocationOption(getDefaultOption());
        this.mLocationClient.setLocationListener(this);
    }

    public void setOnLocationListener(OnLocationListener listener){
        mOnLocationListener = listener;
    }



    public void startLocation() {
        // 启动定位
        if (mLocationClient != null) {
            mLocationClient.startLocation();
        } else {
            Logger.e("startLocation--->mLocationClient is null");
        }
    }


    public void stopLocation() {
        // 停止定位
       if(mLocationClient!=null) {
           mLocationClient.stopLocation();
       }else {
           Logger.e("stopLocation---->mLocationClient is null");
       }
    }

    public void destroyLocation() {
        if (null != mLocationClient) {
            /**
             * 如果AMapLocationClient是在当前Activity实例化的，
             * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
             */
            mLocationClient.onDestroy();
            mLocationClient = null;
        }
    }

    /**
     * 设置定位参数
     * @return
     */
    private AMapLocationClientOption getDefaultOption() {
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setOnceLocation(true);//可选，设置是否单次定位。默认是false
        return mOption;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (null != aMapLocation) {
            if (aMapLocation.getErrorCode() == AMapLocation.LOCATION_SUCCESS) {
                Log.e("LocationUtil","location success->\nlatitude is "+aMapLocation.getLatitude()+"\nlongitude is "+aMapLocation.getLongitude());
                LocationPreferences.getInstance().setLocationLatitude(String.valueOf(aMapLocation.getLatitude()));
                LocationPreferences.getInstance().setLocationLongitude(String.valueOf(aMapLocation.getLongitude()));
                if(mOnLocationListener != null){
                    mOnLocationListener.onLocationSuccess(aMapLocation.getLatitude(),aMapLocation.getLongitude());
                }
            } else {
                Logger.e("定位失败 错误码是" + aMapLocation.getErrorCode()+"\n错误信息是："+aMapLocation.getErrorInfo());
                if(mOnLocationListener != null){
                    mOnLocationListener.onLocationFailed(aMapLocation.getErrorInfo());
                }
            }

        } else {
            Logger.e("定位失败 ----->aMapLocation对象为null");
           if(mOnLocationListener != null){
               mOnLocationListener.onLocationFailed("定位失败");
           }
        }
    }

    public interface OnLocationListener {

        void onLocationSuccess(Double latitude ,Double longitude);

        void onLocationFailed(String errMessage);
    }
}
